#include <conio.h>
#include <vector>
#include "button.h"
#include "filesys.h"
#include "widget.h"

void drawMainWindow();
void drawButton(List *head);

int main()
{
	List *head = NULL;
	initgraph(640, 500);
	HWND hwnd = GetHWnd();
	SetWindowText(hwnd, "职工信息管理系统Alpha V1.0");
	openFile(head);
	drawMainWindow();
	drawButton(head);
	closegraph();
	return 0;
}

void drawMainWindow()
{
	IMAGE cuit,mczs;
	const int imgsize = 128;
	loadimage(&cuit, _T("cuit.jpg"),imgsize,imgsize);
	loadimage(&mczs, _T("mczs.jpg"),imgsize,imgsize);
	setbkcolor(WHITE);
	cleardevice();
	settextstyle(30,0,_T("MiSans"));
	settextcolor(BLACK);
	outtextxy(225, 50, "职工信息管理系统");
	putimage((640-imgsize*2)/2, 500-imgsize, &cuit);
	putimage((640-imgsize*2)/2+imgsize, 500-imgsize, &mczs);
}

void drawButton(List *head)
{
	vector<Button*> Buttons;
	Buttons.push_back(new Button(150, 200, 75, 50, "录入"));
	Buttons.push_back(new Button(225, 200, 75, 50, "修改"));
	Buttons.push_back(new Button(150, 250, 150, 50, "查看总表"));
	Buttons.push_back(new Button(350, 250, 150, 50, "搜索"));
	Buttons.push_back(new Button(350, 200, 150, 50, "删除"));
	Buttons.push_back(new Button(150, 300, 150, 50, "版权信息"));
	Buttons.push_back(new Button(350, 300, 150, 50, "退出系统"));
	ExMessage m;
	IMAGE memImage;
	getimage(&memImage, 0, 0, 800, 600);
	while (true)
	{
		SetWorkingImage(&memImage);
		cleardevice();
		if (peekmessage(&m, EM_MOUSE))
		{
			if(Buttons[0]->isInButton(m)&&m.message==WM_LBUTTONDOWN) insertWidget(head);
			if(Buttons[1]->isInButton(m)&&m.message==WM_LBUTTONDOWN) editWidget(head);
			if(Buttons[2]->isInButton(m)&&m.message==WM_LBUTTONDOWN) showAllWidget(head);
			if(Buttons[3]->isInButton(m)&&m.message==WM_LBUTTONDOWN)
			{
				int searchid = MessageBox(NULL, "是否按照工号查询?\n选“否”则按学历查询", "提示", MB_ICONINFORMATION | MB_YESNO);
				searchWidget(head,searchid);
			}
			if(Buttons[4]->isInButton(m)&&m.message==WM_LBUTTONDOWN) deleteWidget(head),writeFile(head);
			if(Buttons[5]->isInButton(m)&&m.message==WM_LBUTTONDOWN) copyRightWidget();
			if(Buttons[6]->isInButton(m)&&m.message==WM_LBUTTONDOWN) writeFile(head),exit(0);
		}
		drawMainWindow();
		for(auto bt : Buttons) bt->createButton();
		SetWorkingImage(NULL);
		putimage(0, 0, &memImage);
	}
}
